#!/bin/sh

PIDFILE="/var/run/odhcp6c_$IFACE.pid"

. /usr/share/common

if [ ! -d "/sys/class/net/$IFACE" ]; then
	echo_error "$IFACE not found"
	exit 0
fi

if [ "$(sysctl -n net.ipv6.conf.all.disable_ipv6)" -ne 0 ]; then
	echo_error "IPv6 disabled globally"
	exit 0
fi

if [ "true" != "$IF_DHCP_V6_ENABLED" ]; then
	echo_error "DHCPv6 disabled on $IFACE"
	exit 0
fi

echo_info "DHCPv6 on $IFACE"

start_post_up() {
	echo_info "Starting odhcp6c on $IFACE"

	TMP_LOG="/tmp/odhcp6c.log"

	/sbin/odhcp6c -d -v -p "$PIDFILE" -t120 "$IFACE"
}

stop_pre_down() {
	echo_title "Stopping odhcp6c on $IFACE"

	if [ -f "$PIDFILE" ]; then
		pid=$(cat $PIDFILE)
		echo_info "Running service with PID $pid"

		echo_command "kill -9 $pid"
		kill -9 $pid

		echo_command "rm -f $PIDFILE"
		rm -f "$PIDFILE"
	else
		echo_info "Nothing to stop"
	fi
}

case "$MODE" in
	start)
		case "$PHASE" in
			post-up)
				start_post_up
				;;
		esac
		;;

	stop)
		case "$PHASE" in
			pre-down)
				stop_pre_down
				;;
		esac
		;;

	*)
		echo "Usage: $0 {start|stop}"
		exit 0
		;;
esac

exit 0
